<?php

namespace App\Services;

use App\Exceptions\SysExceptions;
use FddCloud\bean\req\signTask\ActorGetUrlReq;
use FddCloud\bean\req\signTask\CreateWithTemplateReq;
use FddCloud\bean\req\signTask\FillFieldValuesReq;
use FddCloud\bean\req\signTask\GetV3ActorSignTaskUrlReq;
use FddCloud\bean\req\template\docTemplate\CreateDocTemplateReq;
use FddCloud\bean\req\template\signTemplate\SignTemplateDetailReq;
use FddCloud\bean\req\template\TemplateCreateGetUrl;
use FddCloud\bean\req\user\AddUserReq;
use FddCloud\bean\req\user\GetUserAuthUrlReq;
use FddCloud\client\Client;
use FddCloud\client\ServiceClient;
use FddCloud\client\SignTaskClient;
use FddCloud\client\TemplateClient;
use FddCloud\client\UserClient;

class FadadaServices
{

    protected static $config = [
        'appID'=>'80002232',
        'appSecret'=>'ZC1PSADTYDUPITM6D5XJI3C19I4ETLOO',
        'url'=>'https://uat-api.fadada.com/api/v5',
        'timeout'=>10,
        'debug'=>true,
    ];

    //缓存的前缀
    protected static $prefix = 'fadada';

    public static function getAccessToken(){
        $client = new Client(
            self::$config['appID'],self::$config['appSecret'],self::$config['url'],self::$config['timeout'],self::$config['debug']
        );
        $serviceClient = new ServiceClient($client);
        $response = $serviceClient->getAccessToken();
        //print_r($response . "\n");
        $res = json_decode($response,true);
        //var_dump($res);
        if(checkIsHasField($res,'code') && $res['code']==100000){
            Cache::setCache(self::$prefix,$res['data']['accessToken'],$res['data']['expiresIn']);
        }else{
            throw new SysExceptions('法大大accessToken错误');
        }
    }

    /**
     * 企业对个人授权
     * @return void
     */
    public static function fadada_user_auth($params){
        $client = new Client(
            self::$config['appID'],self::$config['appSecret'],self::$config['url'],self::$config['timeout'],self::$config['debug']
        );
        $serviceClient = new UserClient($client);
        $accessToken  = Cache::getCacheValue(self::$prefix);
        $reqObj = new GetUserAuthUrlReq();
        $reqObj->setClientUserId('123bvd');
        $reqObj->setRedirectUrl(route('fadada'));
        $res = $serviceClient->getUserAuthUrl($accessToken,$reqObj);
        var_dump($res);

    }

    /**
     * 创建签署
     * @return void
     */
    public static function create_with_template(){
        $client = new Client(
            self::$config['appID'],self::$config['appSecret'],self::$config['url'],self::$config['timeout'],self::$config['debug']
        );
        $serviceClient = new SignTaskClient($client);
        $accessToken  = Cache::getCacheValue(self::$prefix);
        $reqObj = new CreateWithTemplateReq();
        $reqObj->setSignTemplateId('1732507094069176462');
        $reqObj->setInitiator([
            'idType'=>'corp',
            'openId'=>'c147032d696042979fe57b7e5b8ccefc'
        ]);
        $reqObj->setSignTaskSubject('测试合同');
        $reqObj->setAutoStart(false);
        $reqObj->setActors(['actor'=>[
            'actorId'=>'参与方1',
            'actorType'=>'person',
            'actorName'=>'参与方名字',
            'permissions'=>['sign']
        ]
        ]);
        $res = $serviceClient->createWithTemplate($accessToken,$reqObj);
        var_dump($res);
    }

    /**
     * 获取模版详情
     */
    public static function get_template_info(){
        $client = new Client(
            self::$config['appID'],self::$config['appSecret'],self::$config['url'],self::$config['timeout'],self::$config['debug']
        );
        $accessToken  = Cache::getCacheValue(self::$prefix);
        $reqObj = new SignTemplateDetailReq();
        $reqObj->setOwnerId([
            'idType'=>'corp',
            'openId'=>'c147032d696042979fe57b7e5b8ccefc'
        ]);
        $reqObj->setSignTemplateId('1732507094069176462');
        $serviceClient = new TemplateClient($client);
        $res = $serviceClient->getSignTemplateDetail($accessToken,$reqObj);

        $json_data = json_decode($res,TRUE);
        $dataDoc = $json_data['data']['docs'];
        $resultData = [];
        foreach ($dataDoc[0]['docFields'] as $value){
            $resultData[] = [
                'fieldId'=>$value['fieldId'],
                'fieldName'=>$value['fieldName'],
                'fieldValue'=>'abc',
                'docId'=>$dataDoc[0]['docId']
            ];
        }
        return [
            'data_re' => $resultData
        ];


    }

    /**
     * 填充模版内容
     * @return void
     */
    public static function fill_template_content($data_re = []){
        $client = new Client(
            self::$config['appID'],self::$config['appSecret'],self::$config['url'],self::$config['timeout'],self::$config['debug']
        );
        $accessToken  = Cache::getCacheValue(self::$prefix);
        $reqObj = new FillFieldValuesReq();
        $reqObj->setSignTaskId('1733280646942149595');

        $reqObj->setDocFieldValues($data_re);
        $serviceClient = new SignTaskClient($client);
        $res = $serviceClient->fillFieldsValue($accessToken,$reqObj);
        var_dump($res);
    }

    /**
     * 获取签署方链接
     * @return void
     */
    public static function get_sign_url(){
        //1733280646942149595
        $client = new Client(
            self::$config['appID'],self::$config['appSecret'],self::$config['url'],self::$config['timeout'],self::$config['debug']
        );
        $serviceClient = new SignTaskClient($client);
        $accessToken  = Cache::getCacheValue(self::$prefix);
        $reqObj = new ActorGetUrlReq();
        $reqObj->setSignTaskId('1733280646942149595');
        $reqObj->setActorId('参与方1');
        $reqObj->setClientUserId('123bvd');
        $res = $serviceClient->actorGetUrl($accessToken,$reqObj);
        var_dump($res);
    }
}





